//
//  HWGameLiveHttpManager.swift
//  PlayWithProject
//
//  Created by 王昌健 on 2021/10/25.
//

import UIKit
import Alamofire
class HWGameLiveHttpManager: NSObject {
    
    var uid = HWLocallyStoredData.getLoginUserID()
    var tStr = "\(WMUtil.getTimeSp())\(WMUtil.getRandomNumber(101, to: 998))"
    var p = 1
    var max_p = "1"
    
    override init() {
        super.init()
    }
    
    
    /// sign加密
    /// - Returns: sign
    func encrySign() -> String {
        
        let sign = "\(uid)\(tStr)\("fsd213ewdsadqwe2121213edsad")"
        let signMD5 = WMUtil.md5HexDigest(sign) ?? ""
        return signMD5
    }
    func headers() -> HTTPHeaders {
//        let tStr = Date().timeStamp
//        let signature = "nnmitu"+tStr
//
//        let json = ["usersid":HWLocallyData.getUsersID(),"timestamp":tStr,"signature":signature.DDMD5Encrypt(.lowercase32)]
//        let jsonString = HWPublicFuncTools.convertDictionAryToJson(dic: json as NSDictionary)
//        let aesStr:String = aesEncryptString(jsonString, "NrrynIXcpucYvGSk")
        let header:HTTPHeaders = ["eaua":JJUUIDManager.getUUID(),"imei":ASIdentifierManager.shared().advertisingIdentifier.uuidString]
        
        return header
    }
    
    /// 获取标签
//    func getLiveTag(success:@escaping ((_ dataArr:[HWPlayWithTagModel]) -> Void),failure:@escaping((_ errorMessage:String)->Void)) {
//        AF.request(URL(string: URL_LiveClassify)!, method: .post, parameters: nil).responseString { (responses) in
//            if responses.value != nil {
//                let result:String = responses.value ?? ""
//                if result != "﻿" {
//
//                    let resultDic = HWUniversal.JSONStringToDictionary(jsonString: result)
//                    if resultDic["code"] as? Int == 1 {
//
//                        print("***********\(HWUniversal.JSONStringToDictionary(jsonString: result))")
//                        let str =   HWUniversal.ObjectToJSONString(object: resultDic["data"] ?? "")
//                        if let models = [HWPlayWithTagModel].deserialize(from: str) {
//                            print(models)
//                            success((models as? [HWPlayWithTagModel])!)
//
//                        }
//
//
//                    }else {
//                        print("请求失败--原因:\(resultDic["message"] ?? "请求失败--未知原因")")
//                    }
//
//                }else {
//                    print("没有获取到数据")
//                }
//            }else{
//                failure("获取数据失败")
//            }
//        }
//    }
    
    /// 房间列表
    
    func getGameLiveRooms(para:NSDictionary, success:@escaping (_ :[HWGameLiveListModel]) -> Void, failure:@escaping(_ :String)->Void){
        p = 1
        tStr = "\(WMUtil.getTimeSp())\(WMUtil.getRandomNumber(101, to: 998))"
        uid = HWLocallyStoredData.getLoginUserID()
        var keyword = ""
        if let val = para["keyword"] {
            keyword = val as! String
        }
        let parameters  = [
            "uid":uid,
            "t":tStr,
            "sign":encrySign(),
//            "type":para["type"] as! String,
            "p":"\(p)",
            "num":"20",
            "keyword":keyword
        ]

        AF.request(URL(string: URL_LiveList)!, method: .post, parameters: parameters, headers: headers()).responseString { (responses) in

            if responses.value != nil {

                let result:String = responses.value ?? ""
                if result != "" {

                    let resultDic = HWUniversal.JSONStringToDictionary(jsonString: result)
                    if resultDic["code"] as? Int == 1 {


                        print("***********\(HWUniversal.JSONStringToDictionary(jsonString: result))")
                        if let model = [HWGameLiveListModel].deserialize(from: result ,designatedPath: ".data") {
                            print(model)

                            success(model as! [HWGameLiveListModel])

                        }

                    } else if resultDic["code"] as? Int == 404 {
//                        HWUniversal.reLogin(message: (resultDic["message"] as? String) ?? "")
                    }else {
                        print("请求失败--原因:\(resultDic["message"] ?? "请求失败--未知原因")")
                        failure(resultDic["message"] as? String ?? "")
                    }

                }else {
                    print("没有获取到数据")
                }

            }else {
                print("获取数据失败")
                failure("获取数据失败")
            }
        }
    }
    
    /// 房间列表
    
    func getGameLiveRoomsFooter(para:NSDictionary, success:@escaping (_ :[HWGameLiveListModel]) -> Void, failure:@escaping(_ :String)->Void){
        p += 1
        tStr = "\(WMUtil.getTimeSp())\(WMUtil.getRandomNumber(101, to: 998))"
        uid = HWLocallyStoredData.getLoginUserID()

        let parameters  = [
            "uid":uid,
            "t":tStr,
            "sign":encrySign(),
//            "type":para["type"] as! String,
            "p":"\(p)",
            "num":"20"
        ]

        AF.request(URL(string: URL_LiveList)!, method: .post, parameters: parameters, headers: headers()).responseString { (responses) in

            if responses.value != nil {

                let result:String = responses.value ?? ""
                if result != "" {

                    let resultDic = HWUniversal.JSONStringToDictionary(jsonString: result)
                    if resultDic["code"] as! Int == 1 {


                        print("***********\(HWUniversal.JSONStringToDictionary(jsonString: result))")
                        if let model = [HWGameLiveListModel].deserialize(from: result ,designatedPath: ".data") {
                            print(model)

                            success(model as! [HWGameLiveListModel])

                        }

                    } else if resultDic["code"] as? Int == 404 {
//                        HWUniversal.reLogin(message: (resultDic["message"] as? String) ?? "")
                    }else {
                        self.p -= 1
                        print("请求失败--原因:\(resultDic["message"] ?? "请求失败--未知原因")")
                        failure(resultDic["message"] as? String ?? "")
                    }

                }else {
                    print("没有获取到数据")
                    self.p -= 1
                }

            }else {
                print("获取数据失败")
                failure("获取数据失败")
                self.p -= 1
            }
        }
    }
    
    /// 获取IM信息
    @objc func getGameLiveImInfo(success:@escaping (_ :NSDictionary) -> Void, failure:@escaping(_ :String)->Void){
        tStr = "\(WMUtil.getTimeSp())\(WMUtil.getRandomNumber(101, to: 998))"
        uid = HWLocallyStoredData.getLoginUserID()
        
        let parameters  = [
            "uid":uid,
            "t":tStr,
            "sign":encrySign(),
        ]
        
        AF.request(URL(string: URL_LiveLoginInfo)!, method: .post, parameters: parameters, headers: headers()).responseString { (responses) in
            
            if responses.value != nil {
                
                let result:String = responses.value ?? ""
                if result != "" {
                    
                    let resultDic = HWUniversal.JSONStringToDictionary(jsonString: result)
                    if resultDic["code"] as? Int == 1 {
                        
                        
                        print("***********\(HWUniversal.JSONStringToDictionary(jsonString: result))")
                        success(resultDic["data"] as? NSDictionary ?? [:])
//                        if let model = HWVoiceRoomInfoModel.deserialize(from: result ,designatedPath: ".data") {
//                            print(model)
//
//                            success(model)
//
//                        }
                        
                    } else if resultDic["code"] as? Int == 404 {
//                        HWUniversal.reLogin(message: (resultDic["message"] as? String) ?? "")
                    }else {
                        print("请求失败--原因:\(resultDic["message"] ?? "请求失败--未知原因")")
                        failure(resultDic["message"] as? String ?? "")
                    }
                    
                }else {
                    print("没有获取到数据")
                }
                
            }else {
                print("获取数据失败")
                failure("获取数据失败")
            }
        }
    }
    
    /// 获取IM信息
    @objc func getGameLiveConfig(success:@escaping (_ :String) -> Void, failure:@escaping(_ :String)->Void){
        tStr = "\(WMUtil.getTimeSp())\(WMUtil.getRandomNumber(101, to: 998))"
        uid = HWLocallyStoredData.getLoginUserID()
        
        let parameters  = [
            "uid":uid,
            "t":tStr,
            "sign":encrySign(),
        ]
        
        AF.request(URL(string: URL_LiveConfig)!, method: .post, parameters: parameters, headers: headers()).responseString { (responses) in
            
            if responses.value != nil {
                
                let result:String = responses.value ?? ""
                if result != "" {
                    
                    let resultDic = HWUniversal.JSONStringToDictionary(jsonString: result)
                    if resultDic["code"] as? String == "1" {
                        
                        
                        print("***********\(HWUniversal.JSONStringToDictionary(jsonString: result))")
                        success((resultDic["data"] as? NSDictionary ?? [:])["live_appid"] as? String ?? "")
//                        if let model = HWVoiceRoomInfoModel.deserialize(from: result ,designatedPath: ".data") {
//                            print(model)
//
//                            success(model)
//
//                        }
                        
                    } else if resultDic["code"] as? Int == 404 {
//                        HWUniversal.reLogin(message: (resultDic["message"] as? String) ?? "")
                    }else {
                        print("请求失败--原因:\(resultDic["message"] ?? "请求失败--未知原因")")
                        failure(resultDic["message"] as? String ?? "")
                    }
                    
                }else {
                    print("没有获取到数据")
                }
                
            }else {
                print("获取数据失败")
                failure("获取数据失败")
            }
        }
    }
    
    /// 获取直播间信息
    func getGameLiveRoomInfo(roomId:String, success:@escaping (_ :HWVoiceRoomInfoModel) -> Void, failure:@escaping(_ :String)->Void){
        tStr = "\(WMUtil.getTimeSp())\(WMUtil.getRandomNumber(101, to: 998))"
        uid = HWLocallyStoredData.getLoginUserID()
        
        let parameters  = [
            "uid":uid,
            "t":tStr,
            "sign":encrySign(),
            "room_id":roomId,
            "member_id":HWLocallyStoredData.getCode()
        ]
        
        AF.request(URL(string: URL_LiveInfo)!, method: .post, parameters: parameters, headers: headers()).responseString { (responses) in
            
            if responses.value != nil {
                
                let result:String = responses.value ?? ""
                if result != "" {
                    
                    let resultDic = HWUniversal.JSONStringToDictionary(jsonString: result)
                    if resultDic["code"] as? Int == 1 {
                        
                        
                        print("***********\(HWUniversal.JSONStringToDictionary(jsonString: result))")
                        if let model = HWVoiceRoomInfoModel.deserialize(from: result ,designatedPath: ".data") {
                            print(model)
                            
                            success(model)
                            
                        }
                        
                    } else if resultDic["code"] as? Int == 404 {
//                        HWUniversal.reLogin(message: (resultDic["message"] as? String) ?? "")
                    }else {
                        print("请求失败--原因:\(resultDic["message"] ?? "请求失败--未知原因")")
                        failure(resultDic["message"] as? String ?? "")
                    }
                    
                }else {
                    print("没有获取到数据")
                }
                
            }else {
                print("获取数据失败")
                failure("获取数据失败")
            }
        }
    }
    
    ///排行榜标题
    func getRankListTitle(roomId:String, success:@escaping (_ :NSDictionary) -> Void, failure:@escaping(_ :String)->Void){
        
        tStr = "\(WMUtil.getTimeSp())\(WMUtil.getRandomNumber(101, to: 998))"
        uid = HWLocallyStoredData.getLoginUserID()
        
        let parameters  = [
            "uid":uid,
            "t":tStr,
            "sign":encrySign(),
            "room_id":roomId,
            "member_id":HWLocallyStoredData.getCode()
        ]
        
        AF.request(URL(string: URL_GameliveRankTitle)!, method: .post, parameters: parameters, headers: headers()).responseString { (responses) in
            
            if responses.value != nil {
                
                let result:String = responses.value ?? ""
                if result != "" {
                    
                    let resultDic = HWUniversal.JSONStringToDictionary(jsonString: result)
                    if resultDic["code"] as? Int == 1 {
                        
                        
                        print("***********\(HWUniversal.JSONStringToDictionary(jsonString: result))")
                        //                        if let model = [HWVoiceRoomGiftListModel].deserialize(from: result ,designatedPath: ".data") {
                        //                            print(model)
                        //
                        //                            success(model as! [HWVoiceRoomGiftListModel])
                        //
                        //                        }
                        let arr:NSArray = resultDic["data"] as? NSArray ?? []
                        success((arr[0] as! NSDictionary))
                        
                    } else if resultDic["code"] as? Int == 404 {
                        HWUniversal.reLogin(message: (resultDic["message"] as? String) ?? "")
                    }else {
                        
                        print("请求失败--原因:\(resultDic["message"] ?? "请求失败--未知原因")")
                        failure(resultDic["message"] as? String ?? "")
                    }
                    
                }else {
                    print("没有获取到数据")
                    
                }
                
            }else {
                print("获取数据失败")
                //                self.p -= 1
            }
        }
    }
    ///排行榜
    func getRankList(roomId:String,type:NSInteger, success:@escaping (_ :[HWVoiceRoomRankListModel]) -> Void, failure:@escaping(_ :String)->Void){
        
        tStr = "\(WMUtil.getTimeSp())\(WMUtil.getRandomNumber(101, to: 998))"
        uid = HWLocallyStoredData.getLoginUserID()
        
        let parameters  = [
            "uid":uid,
            "t":tStr,
            "sign":encrySign(),
            "rooms_id":roomId,
            "type":type,
            "member_id":HWLocallyStoredData.getCode()
        ] as [String : Any]
        
        AF.request(URL(string: URL_GameLiveRankList)!, method: .post, parameters: parameters, headers: headers()).responseString { (responses) in
            
            if responses.value != nil {
                
                let result:String = responses.value ?? ""
                if result != "" {
                    
                    let resultDic = HWUniversal.JSONStringToDictionary(jsonString: result)
                    if resultDic["code"] as? Int == 1 {
                        
                        
                        print("***********\(HWUniversal.JSONStringToDictionary(jsonString: result))")
                        if let model = [HWVoiceRoomRankListModel].deserialize(from: result ,designatedPath: ".data") {
                            print(model)
                            
                            success(model as! [HWVoiceRoomRankListModel])
                            
                        }
                        
                        
                    } else if resultDic["code"] as? Int == 404 {
                        HWUniversal.reLogin(message: (resultDic["message"] as? String) ?? "")
                    }else {
                        
                        print("请求失败--原因:\(resultDic["message"] ?? "请求失败--未知原因")")
                        failure(resultDic["message"] as? String ?? "")
                    }
                    
                }else {
                    print("没有获取到数据")
                    
                }
                
            }else {
                print("获取数据失败")
                //                self.p -= 1
            }
        }
    }
    
//    func getGames(searchString:String,success:@escaping ((_ model:HWGameLiveAllGameModel) -> Void), failure:@escaping(_ errorMessage:String)->Void){
//        tStr = "\(WMUtil.getTimeSp())\(WMUtil.getRandomNumber(101, to: 998))"
//        uid = HWLocallyStoredData.getLoginUserID()
//
//        let parameters  = [
//            "uid":uid,
//            "t":tStr,
//            "sign":encrySign(),
//            "member_id":HWLocallyStoredData.getCode()
//        ]
//
//        AF.request(URL(string: URL_GameLiveAllGames)!, method: .post, parameters: parameters, headers: headers()).responseString { (responses) in
//            if responses.value != nil {
//                let result:String = responses.value ?? ""
//                if result != "﻿" {
//
//                    let resultDic = HWUniversal.JSONStringToDictionary(jsonString: result)
//                    if resultDic["code"] as? Int == 1 {
//
//                        print("***********\(HWUniversal.JSONStringToDictionary(jsonString: result))")
//                        if let models = HWGameLiveAllGameModel.deserialize(from: result) {
//                            print(models)
//                            success(models)
//                        }
//
//                    } else if resultDic["code"] as? Int == 404 {
//
//                    }else {
//                        failure(resultDic["message"] as? String ?? "")
//                        //                        self.showHUD(title:  resultDic["message"] as! String)
//                        //                        print("请求失败--原因:\(resultDic["message"] ?? "请求失败--未知原因")")
//                    }
//
//                }else {
//                    print("没有获取到数据")
//                }
//            }else{
//                print("获取数据失败")
//            }
//        }
//    }
    
    //查询余额
    func getDiamondNum(success:@escaping (_ :NSDictionary) -> Void, failure:@escaping(_ :String)->Void){

        tStr = "\(WMUtil.getTimeSp())\(WMUtil.getRandomNumber(101, to: 998))"
        uid = HWLocallyStoredData.getLoginUserID()
        
        let parameters  = [
            "uid":uid,
            "t":tStr,
            "sign":encrySign(),
            
        ]
            
        AF.request(URL(string: URL_DiamondNum)!, method: .post, parameters: parameters, headers: headers()).responseString { (responses) in
            
            if responses.value != nil {
                
                let result:String = responses.value ?? ""
                if result != "" {
                    
                    let resultDic = HWUniversal.JSONStringToDictionary(jsonString: result)
                    if resultDic["code"] as? Int == 1 {
                        
                        
                        print("***********\(HWUniversal.JSONStringToDictionary(jsonString: result))")
//                        if let model = [HWVoiceRoomRankListModel].deserialize(from: result ,designatedPath: ".data") {
//                            print(model)
//
//                            success(model as! [HWVoiceRoomRankListModel])
//
//                        }
                        success(resultDic["data"] as! NSDictionary)
                        
                    } else if resultDic["code"] as? Int == 404 {
HWUniversal.reLogin(message: (resultDic["message"] as? String) ?? "")
                    }else {
                        
                        print("请求失败--原因:\(resultDic["message"] ?? "请求失败--未知原因")")
                        failure(resultDic["message"] as? String ?? "")
                    }
                    
                }else {
                    print("没有获取到数据")
                    
                }
                
            }else {
                print("获取数据失败")
                self.p -= 1
            }
        }
    }
    ///获取礼物列表
    func getGiftList(roomId:String, success:@escaping (_ :[HWVoiceRoomGiftListModel]) -> Void, failure:@escaping(_ :String)->Void){

        tStr = "\(WMUtil.getTimeSp())\(WMUtil.getRandomNumber(101, to: 998))"
        uid = HWLocallyStoredData.getLoginUserID()
        
        let parameters  = [
            "uid":uid,
            "t":tStr,
            "sign":encrySign(),
            "room_id":roomId,
            ]
            
        AF.request(URL(string: URL_VoiceRoomGiftList)!, method: .post, parameters: parameters, headers: headers()).responseString { (responses) in
            
            if responses.value != nil {
                
                let result:String = responses.value ?? ""
                if result != "" {
                    
                    let resultDic = HWUniversal.JSONStringToDictionary(jsonString: result)
                    if resultDic["code"] as? Int == 1 {
                        
                        
                        print("***********\(HWUniversal.JSONStringToDictionary(jsonString: result))")
                        if let model = [HWVoiceRoomGiftListModel].deserialize(from: result ,designatedPath: ".data") {
                            print(model)

                            success(model as! [HWVoiceRoomGiftListModel])

                        }
//                        success((resultDic["data"] as! NSDictionary)["room_id"] as! String)
                        
                    } else if resultDic["code"] as? Int == 404 {
HWUniversal.reLogin(message: (resultDic["message"] as? String) ?? "")
                    }else {
                        
                        print("请求失败--原因:\(resultDic["message"] ?? "请求失败--未知原因")")
                        failure(resultDic["message"] as? String ?? "")
                    }
                    
                }else {
                    print("没有获取到数据")
                    
                }
                
            }else {
                print("获取数据失败")
                self.p -= 1
            }
        }
    }
    ///送礼物
    func sendGift(roomId:String,id:String,uids:String,num:String, success:@escaping (_ :String) -> Void, failure:@escaping(_ :String)->Void){

        tStr = "\(WMUtil.getTimeSp())\(WMUtil.getRandomNumber(101, to: 998))"
        uid = HWLocallyStoredData.getLoginUserID()
        
        let parameters  = [
            "uid":uid,
            "t":tStr,
            "sign":encrySign(),
            "rooms_id":roomId,
            "id":id,
            "uids":uids,
            "num":num
        ]
            
        AF.request(URL(string: URL_VoiceRoomSendGift)!, method: .post, parameters: parameters, headers: headers()).responseString { (responses) in
            
            if responses.value != nil {
                
                let result:String = responses.value ?? ""
                if result != "" {
                    
                    let resultDic = HWUniversal.JSONStringToDictionary(jsonString: result)
                    if resultDic["code"] as? Int == 1 {
                        
                        
                        print("***********\(HWUniversal.JSONStringToDictionary(jsonString: result))")
//                        if let model = [HWVoiceRoomRankListModel].deserialize(from: result ,designatedPath: ".data") {
//                            print(model)
//
//                            success(model as! [HWVoiceRoomRankListModel])
//
//                        }
                        success("发送成功")
                        
                    } else if resultDic["code"] as? Int == 404 {
HWUniversal.reLogin(message: (resultDic["message"] as? String) ?? "")
                    }else {
                        
                        print("请求失败--原因:\(resultDic["message"] ?? "请求失败--未知原因")")
                        failure(resultDic["message"] as? String ?? "")
                    }
                    
                }else {
                    print("没有获取到数据")
                    
                }
                
            }else {
                print("获取数据失败")
                
            }
        }
    }
    
    func getGameLiveMyFollow(success:@escaping (_ :[HWGameLiveListModel]) -> Void, failure:@escaping(_ :String)->Void){
        
        tStr = "\(WMUtil.getTimeSp())\(WMUtil.getRandomNumber(101, to: 998))"
        uid = HWLocallyStoredData.getLoginUserID()
        let parameters  = [
            "uid":uid,
            "t":tStr,
            "sign":encrySign(),
        ]

        AF.request(URL(string: URL_LiveMyFollow)!, method: .post, parameters: parameters, headers: headers()).responseString { (responses) in

            if responses.value != nil {

                let result:String = responses.value ?? ""
                if result != "" {

                    let resultDic = HWUniversal.JSONStringToDictionary(jsonString: result)
                    if resultDic["code"] as? Int == 1 {


                        print("***********\(HWUniversal.JSONStringToDictionary(jsonString: result))")
                        if let model = [HWGameLiveListModel].deserialize(from: result ,designatedPath: ".data") {
                            print(model)

                            success(model as! [HWGameLiveListModel])

                        }else {
                            failure("")
                        }

                    } else if resultDic["code"] as? Int == 404 {
//                        HWUniversal.reLogin(message: (resultDic["message"] as? String) ?? "")
                    }else {
                        print("请求失败--原因:\(resultDic["message"] ?? "请求失败--未知原因")")
                        failure(resultDic["message"] as? String ?? "")
                    }

                }else {
                    print("没有获取到数据")
                }

            }else {
                print("获取数据失败")
                failure("获取数据失败")
            }
        }
    }
    
    func getLiveFollow(roomId:String,success:@escaping (_ :String) -> Void, failure:@escaping(_ :String)->Void){
        
        tStr = "\(WMUtil.getTimeSp())\(WMUtil.getRandomNumber(101, to: 998))"
        uid = HWLocallyStoredData.getLoginUserID()
        let parameters  = [
            "uid":uid,
            "t":tStr,
            "sign":encrySign(),
            "rooms_id":roomId
        ]

        AF.request(URL(string: URL_LiveFollow)!, method: .post, parameters: parameters, headers: headers()).responseString { (responses) in

            if responses.value != nil {

                let result:String = responses.value ?? ""
                if result != "" {

                    let resultDic = HWUniversal.JSONStringToDictionary(jsonString: result)
                    if resultDic["code"] as? Int == 1 {


                        print("***********\(HWUniversal.JSONStringToDictionary(jsonString: result))")
//                        if let model = [HWGameLiveListModel].deserialize(from: result ,designatedPath: ".data") {
//                            print(model)
//
//                            success(model as! [HWGameLiveListModel])
//
//                        }else {
//                            failure("")
//                        }

                        success("")
                    } else if resultDic["code"] as? Int == 404 {
//                        HWUniversal.reLogin(message: (resultDic["message"] as? String) ?? "")
                    }else {
                        print("请求失败--原因:\(resultDic["message"] ?? "请求失败--未知原因")")
                        failure(resultDic["message"] as? String ?? "")
                    }

                }else {
                    print("没有获取到数据")
                }

            }else {
                print("获取数据失败")
                failure("获取数据失败")
            }
        }
    }
}

